package com.gjy.concurrency.clazz;

import org.junit.jupiter.api.Test;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-05-18 10:49:24
 */
public class PriorityBlockingQueueTest {

    @Test
    public void test1() throws InterruptedException {
        BlockingQueue<Integer> queue = new PriorityBlockingQueue<>();

        queue.put(1);
        queue.put(2);
        queue.put(3);
        queue.put(4);
    }

    private static final class FIFOEntry<E extends Comparable<? super E>> implements Comparable<FIFOEntry<E>> {
        private static final AtomicLong seq = new AtomicLong(0);
        private final long seqNum;
        private final E entry;

        public FIFOEntry(E entry) {
            seqNum = seq.getAndIncrement();
            this.entry = entry;
        }

        public E getEntry() {
            return entry;
        }

        public int compareTo(FIFOEntry<E> other) {
            int res = entry.compareTo(other.entry);
            if (res == 0 && other.entry != this.entry)
                res = (seqNum < other.seqNum ? -1 : 1);
            return res;
        }
    }

}
